package com.zlf;

import com.zlf.realm.CustomerMD5Realm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

import java.util.Arrays;

/**
 * @ClassName CustomerMD5AuthenticatorTest
 * @Description TODO
 * @Author zlf
 * @Date 2021/5/24 9:40
 */
public class CustomerMD5AuthenticatorTest {

    public static void main(String[] args) {
        CustomerMD5AuthenticatorTest.customerMd5Realm();
    }

    /*
        自定义加密Realm
         */
    private static void customerMd5Realm() {
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        CustomerMD5Realm realm = new CustomerMD5Realm();
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); // 为realm设置凭证匹配器
        credentialsMatcher.setHashAlgorithmName("md5"); // 设置加密算法名称
        credentialsMatcher.setHashIterations(1024); // 设置hash次数
        realm.setCredentialsMatcher(credentialsMatcher);
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken admin = new UsernamePasswordToken("admin", "123456");
        try {
            System.out.println("认证状态：" + subject.isAuthenticated());
            subject.login(admin);
            System.out.println("认证状态：" + subject.isAuthenticated());
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("认证失败：用户不存在！");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("认证失败：密码不正确！");
        } catch (DisabledAccountException e) {
            e.printStackTrace();
            System.out.println("认证失败：账号被禁用！");
        } catch (ExcessiveAttemptsException e) {
            e.printStackTrace();
            System.out.println("认证失败：登录失败次数过多！");
        } catch (ExpiredCredentialsException e) {
            e.printStackTrace();
            System.out.println("认证失败：凭证过期！");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (subject.isAuthenticated()) {
//            //基于角色权限控制
//            System.out.println(subject.hasRole("super"));

//            //基于多角色权限控制(同时具有)
//            System.out.println(subject.hasAllRoles(Arrays.asList("admin", "super")));

            //是否具有其中一个角色
            boolean[] booleans = subject.hasRoles(Arrays.asList("admin", "super", "user"));
            for (boolean aBoolean : booleans) {
                System.out.println(aBoolean);
            }

//            System.out.println("==============================================");
//
//            //基于权限字符串的访问控制  资源标识符:操作:资源类型
//            System.out.println("权限:" + subject.isPermitted("user:update:01"));
//            System.out.println("权限:" + subject.isPermitted("product:create:02"));
//
//            //分别具有那些权限
//            boolean[] permitted = subject.isPermitted("user:*:01", "order:*:10");
//            for (boolean b : permitted) {
//                System.out.println(b);
//            }
//
//            //同时具有哪些权限
//            boolean permittedAll = subject.isPermittedAll("user:*:01", "product:create:01");
//            System.out.println(permittedAll);
        }

    }
}
